window.HttpHelper = {

    /**
     * Get方法
     * @param {请求网址} url 
     * @param {回调} callback 
     * @param {头部参数} headParams 
     */
    Get: function (url, callback,headParams) {

        var xhr = new XMLHttpRequest();

        xhr.onreadystatechange = function () {

            //如果请求状态为Done的话，则执行回调
            if (xhr.readyState == 4) {
   
                if (xhr.status >= 200 && xhr.status < 400) {

                    var response = xhr.responseText;

                    callback(response);

                } else {
                   
                    console.log("请求失败:" + xhr.responseText)

                    callback(false);
                }
                
            }
        };

        xhr.open("GET", url, true);

        if (headParams) {

            for (var key in headParams) {

                xhr.setRequestHeader(key, headParams[key]);
            }
        }

        xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");

        //console.log(url);

        xhr.send();
    },

    Post: function (url, reqData, callback) {

        //拼接请求参数
        var param = "";

        for (var item in reqData) {

            param += item + "=" + reqData[item] + "&";
        }

        //console.log(param);

        //发起请求
        var xhr = new XMLHttpRequest();

        xhr.onreadystatechange = function () {

            if (xhr.readyState == 4) {

                if (xhr.status >= 200 && xhr.status < 400) {

                    var response = xhr.responseText;

                    //console.log(response);        //打印返回数据

                    if (response) {

                        callback(response);

                    } else {

                        console.log("返回数据不存在")

                        callback(false);
                    }

                } else {

                    console.log("请求失败:" + xhr.responseText)

                    callback(false);
                }
            }
        };

        xhr.open("POST", url, true);

        xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");

        xhr.send(param);    //reqData为字符串形式： "key=value"
    },

    PostJson: function (url, params , callback,headParams) {

        //发起请求
        var xhr = new XMLHttpRequest();

        xhr.onreadystatechange = function () {

            if (xhr.readyState == 4) {

                if (xhr.status >= 200 && xhr.status < 400) {

                    var response = xhr.responseText;

                    if (response) {

                        callback(response);

                    } else {

                        console.log("返回数据不存在")

                        callback(false);
                    }

                } else {

                    console.log("请求失败:" + xhr.responseText)

                    callback(false);
                }
            }
        };

        xhr.open("POST", url, true);

        xhr.setRequestHeader('Content-type','application/json; charset=utf-8');

        if (headParams) {

            for (var key in headParams) {
                
                xhr.setRequestHeader(key, headParams[key]);
            }
        }

        xhr.send(JSON.stringify(params ));
    },

    Puts:function (url, reqData, Authorization, callback) {
        var json = JSON.stringify(reqData);
        var xhr = new XMLHttpRequest();
        xhr.open("PUT", url, true);
        xhr.setRequestHeader('Content-type','application/json; charset=utf-8');
        xhr.setRequestHeader('Authorization',Authorization);
        xhr.onload = function () {
        var response = xhr.responseText;
        if (xhr.readyState == 4 && xhr.status == "200") {

                callback(response)
            } else {
                console.log("请求失败:" + xhr.responseText)
                callback(false);
            }
        }
        xhr.send(json);
    },

    /**
     * 上传照片到服务器
     * base64:base64的图片，需要包含头部
     * callback(response),response为返回数据
     */
    UploadPicture(base64, callback) {

        var t_url = "http://platform.qdmedia.cc/Screen3DFrame/getCaptures";

        var data = {

            'img': ImageHelper.urlEncode(base64),
        }

        HttpHelper.Post(t_url, data, function (response) {

            if (!response) {

                callback("ERROR");

            } else {

                callback(response);
            }
        });
    },
}